package cn.bone.ou.user.service.impl;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.StringUtil;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.user.bean.User;
import cn.bone.ou.user.bean.UserApply;
import cn.bone.ou.user.dto.UserDto;
import cn.bone.ou.user.mapper.UserApplyMapper;
import cn.bone.ou.user.mapper.UserMapper;
import cn.bone.ou.user.param.UserParam;
import cn.bone.ou.user.service.IUserService;
import cn.bone.ou.user.vo.UserVo;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserApplyMapper applyMapper;

    /**模拟 - 创建一个游客*/
    public CommonResult<Void> addMock1(Integer communityId){
        User user = new User();
        user.setCommunityId(communityId);
        String s = StringUtil.getUUid();
        user.setWechatId(s);
        user.setNickName(s.substring(0,8));
        user.setHeadImg("http://47.96.109.220/images/1.jpg");
        user.setUsable(1);
        user.setUserType(1);
        Date date = new Date();
        user.setCreateTime(date);
        user.setLoginTime(date);
        userMapper.insert(user);
        return Ref.ok();
    }

    /**模拟 - 游客申请成为住户*/
    public CommonResult<Void> addMock2(Integer userId,Integer houseId){
        User user = userMapper.selectById(userId);
        if(user==null){
            return Ref.error("错误的用户id");
        }
        if(user.getUserType()!=1){
            return Ref.error("该用户不是游客");
        }
        UserApply apply = new UserApply();
        apply.setCommunityId(user.getCommunityId());
        apply.setUserId(userId);
        apply.setApplyType(1);
        apply.setApplyState(0);
        apply.setHouseId(houseId);
        apply.setPhoneNum("13812345678");
        apply.setCreateTime(new Date());
        applyMapper.insert(apply);
        return Ref.ok();
    }

    /**模拟 - 住户申请成为业主*/
    public CommonResult<Void> addMock3(Integer userId){
        User user = userMapper.selectById(userId);
        if(user==null){
            return Ref.error("错误的用户id");
        }
        if(user.getUserType()!=2){
            return Ref.error("该用户不是住户");
        }
        UserApply apply = new UserApply();
        apply.setCommunityId(user.getCommunityId());
        apply.setUserId(userId);
        apply.setApplyType(2);
        apply.setApplyState(0);
        apply.setHouseId(user.getHouseId());
        apply.setPhoneNum(user.getPhoneNum());
        apply.setRealName(StringUtil.getUUid().substring(0,4));
        apply.setIdCard("420101199005051234");
        apply.setIdCardFront("http://47.96.109.220/images/1.jpg");
        apply.setIdCardBack("http://47.96.109.220/images/1.jpg");
        apply.setCreateTime(new Date());
        applyMapper.insert(apply);
        return Ref.ok();
    }

    /**获取列表*/
    public CommonResult<DataGrid<UserVo.ListVo>> list(UserParam.ListParam param) {
        List<UserDto.ListDto> list = userMapper.list(param.getCommunityId(),
                param.getStart(), param.getNum(),
                param.getName(), param.getUserType(), param.getUsable());
        int count = userMapper.countList(param.getCommunityId(),
                param.getName(),param.getUserType(),param.getUsable());

        List<UserVo.ListVo> vos = list.stream().map(dto->{
            UserVo.ListVo vo = new UserVo.ListVo();
            vo.setUserId(dto.getId());
            vo.setNickName(dto.getNickName());
            vo.setHeadImg(dto.getHeadImg());
            vo.setUserType(dto.getUserType());
            vo.setUsable(dto.getUsable());
            vo.setHouseId(dto.getHouseId());
            vo.setBuildNum(dto.getBuildNum());
            vo.setUnitNum(dto.getUnitNum());
            vo.setHouseNum(dto.getHouseNum());
            vo.setPhoneNum(dto.getPhoneNum());
            vo.setRealName(dto.getRealName());
            vo.setGender(dto.getGender());
            vo.setIdCard(dto.getIdCard());
            vo.setIdCardFront(dto.getIdCardFront());
            vo.setIdCardBack(dto.getIdCardBack());
            vo.setCreateTime(dto.getCreateTime()==null?"": TimeUtil.formaterYYYYMMddHHmm(dto.getCreateTime()));
            vo.setLoginTime(dto.getLoginTime()==null?"":TimeUtil.formaterYYYYMMddHHmm(dto.getLoginTime()));
            return vo;
        }).collect(Collectors.toList());
        DataGrid<UserVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        System.out.println(JSON.toJSONString(dataGrid));
        return Ref.ok(dataGrid);
    }

    /**启用用户*/
    @MyLogger(msg = "启用用户")
    public CommonResult<Void> usable(UserParam.UsableParam param) {
        User user = userMapper.selectById(param.getUserId());
        if(user==null){
            return Ref.error("错误的用户id");
        }
        user.setUsable(1);
        userMapper.updateById(user);
        return Ref.ok();
    }

    /**禁用用户*/
    @MyLogger(msg = "禁用用户")
    public CommonResult<Void> disable(UserParam.DisableParam param){
        User user = userMapper.selectById(param.getUserId());
        if(user==null){
            return Ref.error("错误的用户id");
        }
        user.setUsable(0);
        userMapper.updateById(user);
        return Ref.ok();
    }

}
